package org.hamster.multidropbox.main.services
{
	import mx.managers.PopUpManager;
	
	import org.hamster.common.manager.pointcut.events.PointcutEvent;
	import org.hamster.multidropbox.main.constants.AppPopUpButtonConstants;
	import org.hamster.multidropbox.main.events.AppPopUpEvent;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.main.view.AppPopUp;

	public class AppAlertPopUpService
	{
		private static var _instance:AppAlertPopUpService;

		public static function get instance():AppAlertPopUpService
		{
			if (!_instance) {
				_instance = new AppAlertPopUpService();
			}
			return _instance;
		}
		
		public function AppAlertPopUpService()
		{
		}
		
		public function create(title:String, message:String, btnType:uint, handlerList:Array):AppPopUp
		{
			var popUp:AppPopUp = new AppPopUp();
			popUp.title = title;
			popUp.messageLabel = message;
			popUp.buttonDataProvider = btnType;
			popUp.handlerList = handlerList;
			
			popUp.addEventListener(AppPopUpEvent.APPLY_BUTTON_CLICK,  buttonClickHandler, false, 0, true);
			popUp.addEventListener(AppPopUpEvent.CANCEL_BUTTON_CLICK, buttonClickHandler, false, 0, true);
			popUp.addEventListener(AppPopUpEvent.NO_BUTTON_CLICK,     buttonClickHandler, false, 0, true);
			popUp.addEventListener(AppPopUpEvent.OK_BUTTON_CLICK,     buttonClickHandler, false, 0, true);
			popUp.addEventListener(AppPopUpEvent.SAVE_BUTTON_CLICK,   buttonClickHandler, false, 0, true);
			popUp.addEventListener(AppPopUpEvent.YES_BUTTON_CLICK,    buttonClickHandler, false, 0, true);
			
			return popUp;
		}
		
		public function alert(title:String, message:String, btnType:uint, handlerList:Array):void
		{
			AppUtil.addPopup(create(title, message, btnType, handlerList));
		}
		
		private function buttonClickHandler(event:AppPopUpEvent):void
		{
			var buttonType:uint;
			var pointcutEventType:String;
			switch (event.type) {
				case AppPopUpEvent.APPLY_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.APPLY;
					pointcutEventType = PointcutEvent.POINTCUT_NEXT;
					break;
				case AppPopUpEvent.CANCEL_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.CANCEL;
					pointcutEventType = PointcutEvent.POINTCUT_CANCEL;
					break;
				case AppPopUpEvent.NO_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.NO;
					pointcutEventType = PointcutEvent.POINTCUT_CANCEL;
					break;
				case AppPopUpEvent.OK_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.OK;
					pointcutEventType = PointcutEvent.POINTCUT_NEXT;
					break;
				case AppPopUpEvent.SAVE_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.SAVE;
					pointcutEventType = PointcutEvent.POINTCUT_NEXT;
					break;
				case AppPopUpEvent.YES_BUTTON_CLICK:
					buttonType = AppPopUpButtonConstants.YES;
					pointcutEventType = PointcutEvent.POINTCUT_NEXT;
					break;
			}
			
			var popUp:AppPopUp = event.currentTarget as AppPopUp;
			clean(popUp);
			var handler:Function = getHandler(popUp.handlerList, buttonType);
			if (handler != null) {
				var result:* = handler.apply(null, null);
				if (result == false) {
					return;
				}
			}
			
			if (pointcutEventType == PointcutEvent.POINTCUT_NEXT) {
				popUp.pointcutVO.next();
			} else if (pointcutEventType == PointcutEvent.POINTCUT_CANCEL) {
				popUp.pointcutVO.cancel();
			}
		}
		
		private function getHandler(handlerList:Array, type:uint):Function
		{
			if (handlerList) {
				for each (var handler:Object in handlerList) {
					if (handler.type == type) {
						return handler.handler;
					}
				}
			}
			return null;
		}
		
		private function clean(popUp:AppPopUp):void
		{
//			popUp.removeEventListener(AppPopUpEvent.APPLY_BUTTON_CLICK,  buttonClickHandler);
//			popUp.removeEventListener(AppPopUpEvent.CANCEL_BUTTON_CLICK, buttonClickHandler);
//			popUp.removeEventListener(AppPopUpEvent.NO_BUTTON_CLICK,     buttonClickHandler);
//			popUp.removeEventListener(AppPopUpEvent.OK_BUTTON_CLICK,     buttonClickHandler);
//			popUp.removeEventListener(AppPopUpEvent.SAVE_BUTTON_CLICK,   buttonClickHandler);
//			popUp.removeEventListener(AppPopUpEvent.YES_BUTTON_CLICK,    buttonClickHandler);
			PopUpManager.removePopUp(popUp);
		}
		
	}
}